package cn.wolfcode.query;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * @Description: TODO
 * @Author: 伍家钰
 * @Date: 2020/8/10 10:32
 * @Version: 1.0
 */
@Setter
@Getter
public class QueryObject implements Serializable {

    private int currentPage = 1;
    private int pageSize = 10;
    private String keyword;

    //分页设置对象
    private Pageable pageable;

    public Pageable getPageable() {
        if (pageable == null) {
            //没有指定分页对象值, 默认id倒序
            return PageRequest.of(currentPage - 1, pageSize,
                    Sort.Direction.ASC, "_id");
        }
        return pageable;
    }

    public String getKeyword() {
        return StringUtils.hasLength(keyword) ? keyword : null;
    }
}